﻿using System;
using System.Collections.Generic;
using Alphaleonis.Win32.Filesystem;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Runtime.Remoting.Messaging;

namespace DiskClearHelper
{
    public enum EntryType
    {
        File,
        Directory
    }

    public class EntryInfo
    {
        public EntryInfo()
        {
        }

        private long _parentSize;
        public long ParentSzie
        {
            get { return _parentSize; }
            set { _parentSize = value; }
        }

        public string Name { get; set; }

        public EntryType EntryType { get; set; }

        private long _totalSize = -1;

        public double Prencent
        {
            get
            {
                double val = ((double)TotalSize / (double)_parentSize) * 100;
                return val;
            }
        }

        public double TotalSizeGB
        {
            get
            {
                double gb = (double)TotalSize / 1024.0 / 1024 / 1024;
                return gb;
            }
        }

        public long TotalSize
        {
            get
            {
                if(_totalSize == -1)
                {
                    if (EntryType == EntryType.File)
                    {
                        FileInfo fileInfo = new FileInfo(Name);
                        _totalSize = fileInfo.Length;
                    }
                    else
                    {
                        DirectoryInfo directory = new DirectoryInfo(Name);
                        _totalSize = GetCurrentFolderSize(Name);
                    }
                }
                return _totalSize;
            }
        }

        public string FormatTotalSize
        {
            get
            {
                long totalSize = TotalSize;
                return FormatSize(totalSize);
            }
        }

        // 格式化文件大小，显示为 KB, MB, GB
        static string FormatSize(long bytes)
        {
            if (bytes < 1024)
                return $"{bytes} 字节";
            else if (bytes < 1024 * 1024)
                return $"{bytes / 1024.0:F2} KB";
            else if (bytes < 1024 * 1024 * 1024)
                return $"{bytes / (1024.0 * 1024):F2} MB";
            else
                return $"{bytes / (1024.0 * 1024 * 1024):F2} GB";
        }

        static long GetCurrentFolderSize(string folderPath)
        {
            
            var directoryInfo = new DirectoryInfo(folderPath);
            long size = 0;

            try
            {
                // 累加当前文件夹中的文件大小
                foreach (var file in directoryInfo.GetFiles())
                {
                    size += file.Length;
                }

                // 累加子文件夹中的文件大小
                foreach (var dir in directoryInfo.GetDirectories())
                {
                    size += GetCurrentFolderSize(dir.FullName);
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error:"+ex.Message);
            }

            return size;
        }

        //
        public BitmapImage Icon 
        {
            get
            {
                if(EntryType==EntryType.File)
                {
                    var img = new BitmapImage(new Uri("pack://application:,,,/images/file.png", UriKind.Absolute));
                    return img;
                }
                else
                {
                    var img = new BitmapImage(new Uri("pack://application:,,,/images/directory.png", UriKind.Absolute));
                    return img;
                }
            }
        }
            
    }
}
